//
//  LKOpenSubordinateUploadImageViewController.m
//  Mideleague_Agent
//
//  Created by Zhiwang on 2020/6/19.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKOpenSubordinateUploadImageViewController.h"
#import "LKUFPictureRow.h"
#import "LKUFPictureRowCell.h"
@interface LKOpenSubordinateUploadImageViewController ()
@property(nonatomic,strong)UFFormView *formview;
@property(nonatomic,assign)NSInteger pictureTapSelectIndex;
@property(nonatomic,strong)UIImage *selcetedUploadImage;
@end

@implementation LKOpenSubordinateUploadImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"资料照片上传";
    [self setNavgationRightBtn];
    [self createFormView];
    // Do any additional setup after loading the view.
}
-(void)setNavgationRightBtn{
    WS(weakSelf)
    LKBaseButton *rightBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 64, 24) type:UIButtonTypeCustom title:@"完成" titleColor:COLOR_HEX(0x333333) layercorne:13 font:[UIFont fontWithName:@"PingFangSC-Regular" size: 16] backgroudColor:[UIColor clearColor] AndActionBlock:^(UIButton * _Nonnull btn) {
        if (weakSelf.modelBlock) {
            weakSelf.modelBlock(weakSelf.inforModel);
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}
-(void)createFormView{
    WS(weakSelf)
    self.formview = [UFFormView makeFormView:^(UFFormViewMaker * _Nonnull make) {
        make
        .registerRow([LKUFPictureRow class], [LKUFPictureRowCell class])
        .addSection([UFSection makeSection:^(UFSectionMaker * _Nonnull make) {
            make
            .titleStyle([UFRowTitleStyle makeTitleStyle:^(UFRowTitleStyleMaker * _Nonnull make) {
               make
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor)
                .textAlignment(NSTextAlignmentLeft);
            }])
            .valueStyle([UFTextStyle makeTextStyle:^(UFRowTextStyleMaker * _Nonnull make) {
                make
                .textAlignment(NSTextAlignmentRight)
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor);
            }])
            .addRowWithBlock(^__kindof UFRow * _Nonnull{
                LKUFPictureRow *pictureRow = [[LKUFPictureRow alloc] init];
                pictureRow.pictureNumbers = 8;

                pictureRow.picturesArray = [[NSMutableArray alloc] initWithArray:                @[[NSString judegeIsNullAndTransform:weakSelf.inforModel.id_card_img],[NSString judegeIsNullAndTransform:weakSelf.inforModel.back_id_card_img],[NSString judegeIsNullAndTransform:weakSelf.inforModel.hand_id_card_img],[NSString judegeIsNullAndTransform:weakSelf.inforModel.bussiness_img],[NSString judegeIsNullAndTransform:weakSelf.inforModel.agreement_img1],[NSString judegeIsNullAndTransform:weakSelf.inforModel.agreement_img2],[NSString judegeIsNullAndTransform:weakSelf.inforModel.agreement_img3],[NSString judegeIsNullAndTransform:weakSelf.inforModel.agreement_img4]]];;
                pictureRow.titlesPlaceArray = @[@"+身份证正面照",@"+身份证背面照",@"+手持身份证正面照",@"+营业执照",@"+合同第一页",@"+合同第二页",@"+合同第三页",@"+合同第四页"];
                pictureRow.height = 450;
                pictureRow.uploadImageBlock = ^(NSInteger selectValue, UIImageView * _Nonnull selecteImageV) {
                    weakSelf.pictureTapSelectIndex = 0;
                    [weakSelf showAlertChooseVc];
                };
                return pictureRow;
            });
        }])
        .addToSuperView(self.view);
    }];
    [self.formview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view.mas_top).offset(NAVIGATION_BAR_HEIGHT);
        make.bottom.equalTo(self.view);
    }];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info{
    WS(weakSelf)
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {

        UIImage *photo = [info objectForKey:UIImagePickerControllerOriginalImage];
        LKUFPictureRow *row = [self.formview findRowInRange:UFMakeRange(0, self.pictureTapSelectIndex)];
        NSLog(@"选中的%ld",row.selectImageV.tag);
        row.selectImageV.superview.userInteractionEnabled = NO;
        row.selectImageV.image = photo;
            self.selcetedUploadImage = row.selectImageV.image;
            [SVProgressHUD showWithStatus:@"图片上传中..."];
        [[LKNetDataManager defaultManager] sendPOSTRequestWithserverUrl:ServerlUrl apiPath:ImageUploadUrl parameters:nil imageArray:@[self.selcetedUploadImage] targetWidth:100 progress:^(NSProgress * _Nullable progress) {
            
                } success:^(BOOL isSuccess, id  _Nullable responseObject) {
                 row.selectImageV.superview.userInteractionEnabled = YES;
                    [SVProgressHUD dismiss];
                    if ([responseObject[@"code"] isEqualToString:@"success"]) {
                        if ([NSString isBlankString:responseObject[@"data"]]) {
                            [SVProgressHUD showInfoWithStatus:@"图片未上传成功，请重新选择"];
                            return ;
                        }
                        switch (row.selectImageV.tag - 1000) {
                            case 0:
                            {
                                weakSelf.inforModel.id_card_img = responseObject[@"data"];
                            }
                                break;
                            case 1:
                            {
                                weakSelf.inforModel.back_id_card_img = responseObject[@"data"];
                            }
                                break;
                            case 2:
                            {
                                weakSelf.inforModel.hand_id_card_img = responseObject[@"data"];
                            }
                                break;
                            case 3:
                            {
                                weakSelf.inforModel.bussiness_img = responseObject[@"data"];
                            }
                                break;
                            case 4:
                            {
                                weakSelf.inforModel.agreement_img1 = responseObject[@"data"];
                            }
                                break;
                            case 5:
                            {
                                weakSelf.inforModel.agreement_img2 = responseObject[@"data"];
                            }
                                break;
                            case 6:
                            {
                                weakSelf.inforModel.agreement_img3 = responseObject[@"data"];
                            }
                                break;
                            case 7:
                            {
                                weakSelf.inforModel.agreement_img4 = responseObject[@"data"];
                            }
                                break;
                            default:
                                break;
                        }

                    }else{
                       [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                    }
            
                } failure:^(NSString * _Nullable error) {
            
                }];

        
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}

-(void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto infos:(NSArray<NSDictionary *> *)infos{
    WS(weakSelf)
    LKUFPictureRow *row = [self.formview findRowInRange:UFMakeRange(0, self.pictureTapSelectIndex)];
    row.selectImageV.superview.userInteractionEnabled = NO;
    NSLog(@"选中的%ld",row.selectImageV.tag);
    row.selectImageV.image = photos[0];
    self.selcetedUploadImage = row.selectImageV.image;
    [SVProgressHUD showWithStatus:@"图片上传中..."];
    [[LKNetDataManager defaultManager] sendPOSTRequestWithserverUrl:ServerlUrl apiPath:ImageUploadUrl parameters:nil imageArray:@[self.selcetedUploadImage] targetWidth:100 progress:^(NSProgress * _Nullable progress) {
        
            } success:^(BOOL isSuccess, id  _Nullable responseObject) {
             row.selectImageV.superview.userInteractionEnabled = YES;
                [SVProgressHUD dismiss];
                if ([responseObject[@"code"] isEqualToString:@"success"]) {
                    if ([NSString isBlankString:responseObject[@"data"]]) {
                        [SVProgressHUD showInfoWithStatus:@"图片未上传成功，请重新选择"];
                        return ;
                    }
                    switch (row.selectImageV.tag - 1000) {
                        case 0:
                        {
                            weakSelf.inforModel.id_card_img = responseObject[@"data"];
                        }
                            break;
                        case 1:
                        {
                            weakSelf.inforModel.back_id_card_img = responseObject[@"data"];
                        }
                            break;
                        case 2:
                        {
                            weakSelf.inforModel.hand_id_card_img = responseObject[@"data"];
                        }
                            break;
                        case 3:
                        {
                            weakSelf.inforModel.bussiness_img = responseObject[@"data"];
                        }
                            break;
                        case 4:
                        {
                            weakSelf.inforModel.agreement_img1 = responseObject[@"data"];
                        }
                            break;
                        case 5:
                        {
                            weakSelf.inforModel.agreement_img2 = responseObject[@"data"];
                        }
                            break;
                        case 6:
                        {
                            weakSelf.inforModel.agreement_img3 = responseObject[@"data"];
                        }
                            break;
                        case 7:
                        {
                            weakSelf.inforModel.agreement_img4 = responseObject[@"data"];
                        }
                            break;
                        default:
                            break;
                    }

                }else{
                   [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                }
        
            } failure:^(NSString * _Nullable error) {
        
            }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
